﻿using System;
using System.IO;
using System.Web;
using System.Diagnostics;
namespace Indeco.G24.Web
{
    /// <summary>
	/// Default exception to be thrown by the website, it will
	/// automatically log the contents of the exception to the
	/// Windows NT/2000 Application Event Log.
	/// </summary>
    public class AppException : System.Exception
    {
        public AppException()
        {
            LogError("Một lỗi đã xảy ra.");
        }

        public AppException(string message)
        {
            LogError(message);
        }

        public AppException(string message, Exception innerException)
        {
            LogError(message);
            if (innerException != null)
            {
                LogError(innerException.Message);
            }
        }
        public static void ShowError(Exception currentError)
        {
            HttpContext context = HttpContext.Current;

            context.Response.Write(
                "<link rel=\"stylesheet\" href=\"/Webpages/Styles/Blue.CSS\">" +
                "<h2>Lỗi</h2><hr/>Một lỗi đã xuất hiện trên trang này. Hệ thống đã " +
                "thông báo lỗi này cho quản trị hệ thống." +
                "<br/><b>Lỗi xuất hiện trong: </b>" +
                "<pre>" + context.Request.Url.ToString() + "</pre><br/>" +
                "<br/><b>Thông Báo Lỗi: </b>" +
                "<pre>" + currentError.Message.ToString() + "</pre><hr/>" +
                "<br/><b>Stack Trace(Truy vết):</b><br/>" +
                "<pre>" + currentError.ToString() + "</pre>");
        }
        public static void LogError(string message)
        {
            Trace.WriteLine("ewe, LogError=" + message); //qqq

            // Get the current HTTPContext
            HttpContext context = HttpContext.Current;

            // Get location of ErrorLogFile from Web.config file
            string filePath = context.Server.MapPath(
              (string)(System.Configuration.ConfigurationSettings.AppSettings["ErrorLogFile"]));

            Trace.WriteLine("ewe, filepath=" + filePath); //qqq

            // Calculate GMT offset
            int gmtOffset = DateTime.Compare(DateTime.Now, DateTime.UtcNow);

            string gmtPrefix;
            if (gmtOffset > 0)
            {
                gmtPrefix = "+";
            }
            else
            {
                gmtPrefix = "";
            }

            // Create DateTime string
            string errorDateTime = DateTime.Now.Year.ToString() + "." + DateTime.Now.Month.ToString() + "." +
                DateTime.Now.Day.ToString() + " @ " + DateTime.Now.Hour.ToString() + ":" +
                DateTime.Now.Minute.ToString() + ":" + DateTime.Now.Second.ToString() +
                " (GMT " + gmtPrefix + gmtOffset.ToString() + ")";

            // Write message to error file
            try
            {
                StreamWriter sw = new StreamWriter(filePath, true);
                try
                {
                    sw.WriteLine("## " + errorDateTime + " ## " + message + " ##");
                    sw.Flush();
                }
                finally
                {
                    sw.Close();
                }
                //EventViewer EV = new EventViewer();
                //EV.InsertEventView(message, message, '1');
            }
            catch (Exception ex)
            {
                // TODO: This might be a security risk for production. You never want to show
                // end users a detailed error msg and a stack trace. But this helps us in Development.
                ShowError(ex);
            }
        }
    }
}
